import Vue from 'vue'
import Router from 'vue-router'
import { PcCookie, enums } from '../utils'

import home from '@/pages/home/index'
import detail from '@/pages/home/detail'
import shop from '@/pages/home/shop'
import addstore from '@/pages/home/addstore'
import addgoods from '@/pages/home/addgoods'
import goodsdetail from '@/pages/home/goodsdetail'
import address from '@/pages/home/address'
import store from '@/pages/home/store'
import collection from '@/pages/home/collection'
import storeOrder from '@/pages/home/storeOrder'
import myOrder from '@/pages/home/myOrder'
import myMain from '@/pages/home/myMain'
import myGoods from '@/pages/home/myGoods'
import shopList from '@/pages/home/shopList'
import orderDetails from '@/pages/home/orderDetails'
import advertisingList from '@/pages/home/advertising/list'
import advertisingInfo from '@/pages/home/advertising/info'
import advertisingCreate from '@/pages/home/advertising/create'




const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  routes: [
    {path: '/',name: 'base',redirect: 'home'},
    {path: '/home',name: 'home',component: home, meta: { index: 0 ,slide:4}},
    {path: '/detail',name: 'detail',component: detail, meta:{slide:2}},
    {path: '/shop',name: 'shop',component: shop, meta: { index: 1 ,slide:3}},
    {path: '/advertisingList',name: 'advertisingList',component: advertisingList, meta: { index: 2 ,slide: 2}},
    {path: '/advertisingInfo',name: 'advertisingInfo',component: advertisingInfo, meta: { slide: 3}},
    {path: '/advertisingCreate',name: 'advertisingCreate',component: advertisingCreate, meta: { slide: 3}},
    {path: '/addstore',name: 'addstore',component: addstore, meta:{slide:1}},
    {path: '/addgoods',name: 'addgoods',component: addgoods, meta:{slide:1}},
    {path: '/goodsdetail',name: 'goodsdetail',component: goodsdetail, meta:{slide:1}},
    {path: '/address',name: 'address',component: address, meta:{slide:1}},
    {path: '/store',name: 'store',component: store, meta:{slide:1}},
    {path: '/collection',name: 'collection',component: collection, meta:{slide:1}},
    {path: '/storeOrder',name: 'storeOrder',component: storeOrder, meta:{slide:1}},
    {path: '/myOrder',name: 'myOrder',component: myOrder, meta:{slide:1}},
    {path: '/myGoods',name: 'myGoods',component: myGoods, meta:{slide:1}},
    {path: '/myMain',name: 'myMain',component: myMain, meta: { index: 3, slide:2 }},
    {path: '/shopList',name: 'shopList',component: shopList, meta: { slide:2 }},
    {path: '/orderDetails',name: 'orderDetails',component: orderDetails, meta: { slide:3 }},


  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  window.scrollTo(0,0)
  next()
  // 判断是否登录状态，未登录直接跳转到登录页
  // if(judgeIsLogin()) { //已经登录
  //   if(to.name == 'login') {
  //     next('/home')
  //   }else {
  //     next()
  //   }
  // }else { //没有登录
  //   // 如果没有token获取token存入cookie
  //   if(!PcCookie.get(enums.USER.TOKEN)) {
  //     GetToken({Phone: '1'}).then(result => {
  //       if(!!result && typeof result == 'string') {
  //         PcCookie.set({
  //           key: enums.USER.TOKEN,
  //           value: result
  //         })
  //         if(to.name === 'illustrate') {
  //           next();
  //         }else if(to.name != 'login' && to.name != 'register') {
  //           next('/login');
  //         }else {
  //           next();
  //         }
  //       }else {
  //         alert('获取token失败，请退出重试！')
  //       }
  //     })
  //   }else {
  //     if(to.name === 'illustrate') {
  //       next();
  //     }else if(to.name != 'login' && to.name != 'register') {
  //       next('/login');
  //     }else {
  //       next()
  //     }
  //   }
  // }
})

export default router
